package com.ygj.documentProcessing.service.impl;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.base.common.persistent.dao.NotesDAO;
import com.base.common.persistent.dao.SignDAO;
import com.gexin.rp.sdk.base.uitls.Base64Util;
import com.ygj.base.common.persistent.model.Notes;
import com.ygj.base.common.persistent.model.Sign;
import com.ygj.base.webservice.entity.announcement.Note;
import com.ygj.documentProcessing.service.DocumentProcessingService;

@Service("documentProcessingService")
public class DocumentProcessingServiceImpl implements DocumentProcessingService {

	@Resource
	private SignDAO signDao;
	
	@Resource
	private NotesDAO notesDao;
	
	@Override
	public int queryDocumentProcessingTotalCounts(String username) {
		//String sql = "select model.fno from Sign as model where model.fmen = ? and model.fpclass = '审批'";
		return signDao.findByProperties(username, "审批").size();
	}

	@Override
	public List<Sign> queryDocumentProcessing(String username) {
		//String sql = "select model.fno, model.fbm, model.ftitle, model.fdate, model.rowguid from Sign as model where model.fmen = ? and model.fpclass = '审批' order by model.fdate desc";
		return signDao.findByProperties(username, "审批");
	}

	@Override
	public Note queryDocumentProcessingDetail(String fno) {
		Notes notes = notesDao.findByFno(fno);
		Note note = new Note();
		note.setFbm(notes.getFbm());
		note.setFclass(notes.getFclass());
		note.setFcom(notes.getFcom());
		note.setFcon(notes.getFcon());
		note.setFcurmen(notes.getFcurmen());
		note.setFdate(notes.getFdate());
		note.setFfb(notes.getFfb());
		note.setFmen1(notes.getFmen1());
		note.setFmen2(notes.getFmen2());
		note.setFmt(notes.getFmt());
		note.setFmj(notes.getFmj());
		note.setFno(notes.getFno());
		note.setFoc(notes.getFoc());
		note.setFps(notes.getFps());
		note.setFpub(notes.getFpub());
		note.setFpubtime(notes.getFpubtime());
		note.setFseed(notes.getFseed());
		note.setFsend(notes.getFsend());
		note.setFser(notes.getFser());
		note.setFsize(notes.getFsize());
		note.setFsp(notes.getFsp());
		note.setFstate(notes.getFstate());
		note.setFtitle(notes.getFtitle());
		note.setFupdate(notes.getFupdate());
		note.setRowguid(notes.getRowguid());
		note.setFstmStr(Base64Util.getBASE64(notes.getFstm()));
		
		return note;
	}

	@Transactional
	@Override
	public int approve(String fmen, Notes notes) {
		String sql = "";
		
		// 更新fps批示意见
//		sql = "update Notes set fps = ? where fno = ?";
		sql = "update notes set fps = '" + notes.getFps().trim() + "' where fno = '" + notes.getFno().trim() + "'";
		if(notesDao.updateSomeByProperties(sql) == 1){
			
		
			// 改变审批状态
			sql = "update sign set fpclass = '已审' where fpclass = '审批' and fmen = '" + fmen + "' and fno = '" + notes.getFno() + "'";
			if(signDao.updateSomeByProperties(sql) == 1){
				
				// 检查是否是最后审批人
				String[] fspArr = notes.getFsp().trim().split("\r\n");
				if ((fmen.trim()).equals(fspArr[fspArr.length - 1].trim())) {
					
					String[] ffbArr = notes.getFfb().trim().split("\r\n");
					for(String ffb : ffbArr) {
						Sign sign = new Sign();
						sign.setFno(notes.getFno().trim());
						sign.setFtitle(notes.getFtitle());
						sign.setFbm(notes.getFbm());
						SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
						sign.setFdate(dateFormat.format(new Date()));
						sign.setFclass("发布");
						sign.setFpclass("签收");
						sign.setFmen(ffb.trim());
						signDao.save(sign);
					}
					sql = "update notes set fstate='批准', fmen2='" + fmen + "'  where fno = '" + notes.getFno().trim() + "'";
					int i = notesDao.updateSomeByProperties(sql);
					System.out.println(i);
					return 1;
				} else {
					Sign sign = new Sign();
					sign.setFno(notes.getFno().trim());
					sign.setFbm(notes.getFbm());
					int index = -1;
					for(int i = 0; i < fspArr.length; i++){
						if(fmen.equals(fspArr[i])){
							index = i;
							break;
						}
					}
					System.out.println(fspArr[index + 1] + notes.getFno());
					sign.setFmen(fspArr[index + 1]);
					sign.setFtitle(notes.getFtitle());
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
					sign.setFdate(dateFormat.format(new Date()));
					sign.setFclass("审批");
					sign.setFpclass("审批");
					signDao.save(sign);
					return 1;
				}
			} else {
				throw new RuntimeException();
			}
			
		} else {
			throw new RuntimeException();
		}
	}

	@Transactional
	@Override
	public int backToTheDrafters(String fmen, Notes notes) {
		String sql = "";
		
		// 改变审批状态
		sql = "update sign set fpclass = '已退' where fpclass = '审批' and fmen = '" + fmen + "' and fno = '" + notes.getFno().trim() + "'";
		if(signDao.updateSomeByProperties(sql) == 1){
			// 退单
			Sign sign = new Sign();
			sign.setFno(notes.getFno());
			sign.setFmen(notes.getFmen1().trim());
			sign.setFtitle(notes.getFtitle());
			sign.setFbm(notes.getFbm());
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			sign.setFdate(dateFormat.format(new Date()));
			sign.setFclass("退回");
			sign.setFpclass("退回");
			signDao.save(sign);
			sql = "update notes set fps = '" + notes.getFps().trim() + "' where fno = '" + notes.getFno().trim() + "'";
			notesDao.updateSomeByProperties(sql);
			return 1;
		}
		return 0;
		
	}
	
	@Transactional
	@Override
	public int cancelTheOrderToLast(String fmen, Notes notes) {
		String sql = "";
		
		// 改变审批状态
		sql = "update sign set fpclass = '已退' where fpclass = '审批' and fmen = '" + fmen + "' and fno = '" + notes.getFno().trim() + "'";
		if(signDao.updateSomeByProperties(sql) == 1){
			// 判断是否有上一级审批人
			String[] fspArr = notes.getFsp().split("\r\n");
			int index = Arrays.binarySearch(fspArr, fmen);
			if(index > 0) {
				// 退单到上一级
				Sign sign = new Sign();
				sign.setFno(notes.getFno());
				sign.setFmen(fspArr[index-1].trim());
				sign.setFbm(notes.getFbm());
				sign.setFtitle(notes.getFtitle());
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				sign.setFdate(dateFormat.format(new Date()));
				sign.setFclass("审批");
				sign.setFpclass("审批");
				signDao.save(sign);
				sql = "update notes set fps = '" + notes.getFps().trim() + "' where fno = '" + notes.getFno().trim() + "'";
				notesDao.updateSomeByProperties(sql);
				return 1;
			} else {
				// 退单到起草人
				Sign sign = new Sign();
				sign.setFno(notes.getFno());
				sign.setFmen(notes.getFmen1().trim());
				sign.setFbm(notes.getFbm());
				sign.setFtitle(notes.getFtitle());
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				sign.setFdate(dateFormat.format(new Date()));
				sign.setFclass("退回");
				sign.setFpclass("退回");
				signDao.save(sign);
				sql = "update notes set fps = '" + notes.getFps().trim() + "' where fno = '" + notes.getFno().trim() + "'";
				notesDao.updateSomeByProperties(sql);
				return 1;
			}
		}
		
		return 0;
		
	}

	@Transactional
	@Override
	public int updateFsp(String fmen, String fno, String ftitle, String fbm, String oldFsp, String newFsp, String newFfb) {
		
		String sql = "";
		
		sql = "update notes set fsp = '" +  newFsp + "', ffb = '"  + newFfb+ "' where fno = '" + fno.trim() + "'";
		notesDao.updateSomeByProperties(sql);
		
		String[] oldFspArr = oldFsp.trim().split("\r\n");
		String[] newFspArr = newFsp.trim().split("\r\n");
		
		sql = "delete from sign where fpclass = '审批' and fmen = '" + fmen + "' and fno = '" + fno.trim() + "'";
		signDao.deleteBySql(sql);
		
		Sign sign = new Sign();
		sign.setFno(fno.trim());
		sign.setFbm(fbm);
		int index = -1;
		for(int i = 0; i < oldFspArr.length; i++){
			if(fmen.equals(oldFspArr[i])){
				index = i;
				break;
			}
		}
		System.out.println(newFspArr[index] + fno);
		sign.setFmen(newFspArr[index]);
		sign.setFtitle(ftitle);
		sign.setFbm(fbm);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		sign.setFdate(dateFormat.format(new Date()));
		sign.setFclass("审批");
		sign.setFpclass("审批");
		signDao.save(sign);
		return 1;
	}
	
	/*@Test
	public void test(){
		String[] aa = {"a", "b", "v"};
		System.out.println(Arrays.binarySearch(aa, "v"));
	}*/

}
